/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package figures.basic;

import figures.Colors;
import figures.Figure;
import geometry.MovingTransform;
import geometry.Point;
import geometry.Rectangle;
import image.GisImage;
import map.file.FileConstants;
import screen.GisGraphics;

public class ImageFigure extends Rectangle implements Figure{

	private int x;
	private int y;
	private GisImage im=null;
	public byte[] n = null;
	
	public ImageFigure(){
	}
	
	public ImageFigure(GisImage i, Point point) {
		super(point.x-i.getWidth()/2,
			  point.y-i.getHeight()/2,
			  point.x+i.getWidth()/2,
			  point.y+i.getHeight()/2);
		im = i;
		x = point.x;
		y = point.y;
		n = new byte[0];
	}

	public void setImage(GisImage i){
		im = i;
		bbxi = x-i.getWidth()/2;
		bbyi = y-i.getHeight()/2;
		bbxf = x+i.getWidth()/2;
		bbyf = y+i.getHeight()/2;
	}
	
	public boolean equals(Object o){
		if(o==null || o.getClass() != this.getClass())
			return false;
		ImageFigure p = (ImageFigure)o;
		if(x!=p.x || y!=p.y /*|| n.length!=p.n.length*/)
			return false;
		/*for(int i=0;i<n.length;i++)
			if(n[i]!=p.n[i])
				return false;*/
		/*for(int i=0;i<vx.length;i++)
			if(vx[i] != p.vx[i] || vy[i] != p.vx[i])
				return false;*/
		return true;
	}
	
	public void setPos(int xp, int yp){
		x = xp;
		y = yp;
		bbxi = x-im.getWidth()/2;
		bbyi = y-im.getHeight()/2;
		bbxf = x+im.getWidth()/2;
		bbyf = y+im.getHeight()/2;
	}
	
	public void drawIn(GisGraphics g, Rectangle area, MovingTransform transform) {
		g.image(im, transform.mapX2screen(x, y)-im.getWidth()/2, transform.mapY2screen(x, y)-im.getHeight()/2, 0);
	}

	public byte getType() {
		return FileConstants.FIG_TYPE_IMAG;
	}

	public Rectangle getBoundingBox() {
		return this;
	}

	public void setId(int i) {		
	}

	public int getId() {
		return 0;
	}

	public byte[] getName() {
		return n;
	}

	public void setName(String s) {
		n = s.getBytes();
	}

	public void drawVolatileIn(GisGraphics g, Rectangle area, MovingTransform transform, boolean highlight, int frame) {
		if(highlight){
			int off=18-frame%10;
			int l = 6;
			int w=3;
			g.setColor(Colors.highlight);
			int sx = transform.mapX2screen(x, y);
			int sy = transform.mapY2screen(x, y);
			g.drawLine(sx+off, sy, sx+l+off, sy+w);
			g.drawLine(sx+off, sy, sx+l+off, sy-w);
			g.drawLine(sx-off, sy, sx-l-off, sy+w);
			g.drawLine(sx-off, sy, sx-l-off, sy-w);
			g.drawLine(sx, sy+off, sx+w, sy+l+off);
			g.drawLine(sx, sy+off, sx-w, sy+l+off);
			g.drawLine(sx, sy-off, sx+w, sy-l-off);
			g.drawLine(sx, sy-off, sx-w, sy-l-off);
		}
		g.drawString(getName(), x, y, 1, 0);
	}

}
